var app=getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {   
    setting:{}, 
    banrdName:'',
    remark:'',
    mobile:'',
    name:'',
    lianxi:'',
    getUrl:"/api/banrd/isApplyBanrd.do",
	submit:"{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var setting = wx.getStorageSync("pingtai")
    this.setData({
      setting: setting
    })
		  var self = this;
		  ux.request(this.data.getUrl,options,function(protocol){
        if(protocol.data.obj){
        self.setData({
          id:protocol.data.obj.id,
          banrdName: protocol.data.obj.conName ? protocol.data.obj.conName:"",
          remark: protocol.data.obj.remarks ? protocol.data.obj.remarks : "",
          mobile: protocol.data.obj.mobile ? protocol.data.obj.mobile : "",
          name: protocol.data.obj.brandName ? protocol.data.obj.brandName : "",
          lianxi: protocol.data.obj.contacts ? protocol.data.obj.contacts : "",
        });
        }
		  });
  },
  
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },
  banrdName:function(e){
     this.setData({
       banrdName:e.detail.value
     })
  },
  remark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },
  mobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  name: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  lianxi: function (e) {
    this.setData({
      lianxi: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  
  tijiao:function(){
    var self=this;
    if (!this.data.name) {
      this.toast("请填写品牌名称");
      return false;
    }
    if (!this.data.banrdName) {
      this.toast("请填写公司名称");
      return false;
    }
    if (!this.data.lianxi) {
      this.toast("请填写联系人");
      return false;
    }
    if (!this.data.mobile) {
      this.toast("请填写手机号码");
      return false;
    }
    var regMobile = /^1\d{10}$/;
    if (!regMobile.test(this.data.mobile)) {
      this.toast("手机格式有误");
      return false;
    }
    var pamars = {
      name: this.data.name,
      mobile: this.data.mobile,
      lianxi: this.data.lianxi,
      banrdName: this.data.banrdName,
      remark: this.data.remark
    }
    wx.showLoading({
      title: '提交中...',
      mask: true
    })
    var urls="";
    if(this.data.id){
      urls ="/api/banrd/editApplyBanrd.do";
    }else{
      urls = "/api/banrd/saveApplyBanrd.do";
    }
    ux.request(urls, pamars, function (protocol) {
      wx.hideLoading();
      self.toast("提交成功");
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1]; //当前页面
      var prevPage = pages[pages.length - 2]; //上一个页面
      wx.navigateBack({
        delta: 1
      })
    }, true)
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
  tel: function () {
    var self = this;
    wx.makePhoneCall({
      phoneNumber: self.data.setting.number,
    })
  }
})
